package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.mapper.UserBindMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.UserAccountService;
import com.atguigu.srb.core.service.UserBindService;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.util.JwtUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author Atguigu
 * @since 2024-04-15
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserAccountService userAccountService;
    @Override
    public String bindHfb(String token, UserBind userBind) {
        Long userId = JwtUtils.getUserId(token);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("idCard",userBind.getIdCard());
        paramMap.put("personalName",userBind.getName());
        paramMap.put("bankType",userBind.getBankType());
        paramMap.put("bankNo",userBind.getBankNo());
        paramMap.put("mobile",userBind.getMobile());
        paramMap.put("timestamp",System.currentTimeMillis());
        paramMap.put("agentUserId",userId);
        paramMap.put("agentId",HfbConst.AGENT_ID);
        paramMap.put("returnUrl",HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl",HfbConst.USERBIND_NOTIFY_URL);
        String sign = RequestHelper.getSign(paramMap);
        paramMap.put("sign", sign); // 签名： 保证数据不会被前端篡改 -> 通过秘钥和加密算法对数据处理得到
        String form = FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);
        // 用户id应该保证唯一性
        UserBind userBind1 = this.getOne(Wrappers.lambdaQuery(UserBind.class).eq(UserBind::getUserId, userId));
        if(userBind1 == null){
            userBind1 = new UserBind();
        }
        userBind1.setUserId(userId);
        userBind1.setName(userBind.getName());
        userBind1.setIdCard(userBind.getIdCard());
        userBind1.setBankNo(userBind.getBankNo());
        userBind1.setBankType(userBind.getBankType());
        userBind1.setMobile(userBind.getMobile());
        userBind1.setStatus(0);
        this.saveOrUpdate(userBind1);
        return form;
    }

    @Override
    public String hfbNotify(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = RequestHelper.switchMap(parameterMap);
        String sign = map.get("sign").toString();
        boolean signEquals = RequestHelper.isSignEquals(map);
        if(!signEquals){
            return "error";
        }
        String agentUserId = map.get("agentUserId").toString();
        String bindCode = map.get("bindCode").toString();
        String resultCode = map.get("resultCode").toString();
        if(!"0001".equals(resultCode)){
            return "error";
        }
        String resultMsg = map.get("resultMsg").toString();
        // 绑定成功，我们应该更新user_info和user_bind中的数据
        UserBind userBind = this.getOne(Wrappers.lambdaQuery(UserBind.class).eq(UserBind::getUserId, agentUserId));
        userBind.setStatus(1);
        userBind.setBindCode(bindCode);
        this.updateById(userBind);
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userBind.getUserId());
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfo.setBindStatus(1);
        userInfo.setBindCode(userBind.getBindCode());
        userInfoService.updateById(userInfo);
        // 用户绑定hfb成功以后，我们应该初始化他的账户user_account
        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userInfo.getId());
        userAccount.setAmount(new BigDecimal(0));
        userAccount.setFreezeAmount(new BigDecimal(0));
        userAccountService.save(userAccount);
        return "success";
    }
}
